<template>
    <div>
        <div class="page-title">党建轮播图</div>
        <el-form ref="form" :model="form" label-width="100px">
            <el-form-item label="轮播图1.">
                <el-upload :action="uploadImage" :show-file-list="false" :before-upload="beforeUpload"
                    :on-success="uploadSuccess1">
                    <div v-if="image1" class="relative">
                        <img :src="image1" class="banner">
                        <el-button class="reupload" icon="el-icon-refresh" size="mini">重新上传</el-button>
                    </div>
                    <i v-else class="el-icon-plus"></i>
                    <template #tip>
                        <div class="el-upload__tip">
                            只能上传 PNG / JPG / GIF / BMP / WEBP 文件，且不超过 2MB，建议尺寸 750 x 375
                        </div>
                    </template>
                </el-upload>
            </el-form-item>

            <el-form-item label="轮播图2.">
                <el-upload :action="uploadImage" :show-file-list="false" :before-upload="beforeUpload"
                    :on-success="uploadSuccess2">
                    <div v-if="image2" class="relative">
                        <img :src="image2" class="banner">
                        <el-button class="reupload" icon="el-icon-refresh" size="mini">重新上传</el-button>
                    </div>
                    <i v-else class="el-icon-plus"></i>
                    <template #tip>
                        <div class="el-upload__tip">
                            只能上传 PNG / JPG / GIF / BMP / WEBP 文件，且不超过 2MB，建议尺寸 750 x 375
                        </div>
                    </template>
                </el-upload>
            </el-form-item>

            <el-form-item label="轮播图3.">
                <el-upload :action="uploadImage" :show-file-list="false" :before-upload="beforeUpload"
                    :on-success="uploadSuccess3">
                    <div v-if="image3" class="relative">
                        <img :src="image3" class="banner">
                        <el-button class="reupload" icon="el-icon-refresh" size="mini">重新上传</el-button>
                    </div>
                    <i v-else class="el-icon-plus"></i>
                    <template #tip>
                        <div class="el-upload__tip">
                            只能上传 PNG / JPG / GIF / BMP / WEBP 文件，且不超过 2MB，建议尺寸 750 x 375
                        </div>
                    </template>
                </el-upload>
            </el-form-item>

            <el-form-item>
                <el-button type="success" @click="submit">保存设置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {
        get,
        post
    } from '@/network/request'
    import {
        __URL__
    } from '@/main'

    export default {
        name: 'BannerParty',
        data() {
            return {
                uploadImage: __URL__ + '/uploadImage', // 上传图片地址
                form: {
                    cid: sessionStorage.getItem('cid'),
                    type: 'party',
                    image1: '',
                    image2: '',
                    image3: ''
                },
                image1: '',
                image2: '',
                image3: ''
            }
        },
        mounted() {
            this.$nextTick(() => {
                // 获取信息
                get('/getBanner', {
                    cid: sessionStorage.getItem('cid'),
                    type: 'party'
                }).then(res => {
                    const result = res.data
                    if (result.code == 200) {
                        this.form.image1 = result.data[0]
                        this.form.image2 = result.data[1]
                        this.form.image3 = result.data[2]
                        this.image1 = result.data[0]
                        this.image2 = result.data[1]
                        this.image3 = result.data[2]
                    }
                }).catch()
            })
        },
        methods: {
            // 上传准备
            beforeUpload(file) {
                if (['image/png', 'image/jpeg', 'image/gif', 'image/bmp', 'image/webp'].indexOf(file
                        .type) == -1) {
                    this.$message.error({
                        message: '请上传正确的图片格式！',
                        type: 'error',
                        offset: 70
                    })
                    return false
                }

                const size = file.size / 1024 / 1024 < 2
                if (!size) {
                    this.$message.error({
                        message: '图片大小不能超过 2MB！',
                        type: 'error',
                        offset: 70
                    })
                    return false
                }
            },
            // 上传成功
            uploadSuccess1(res, file) {
                this.form.image1 = res.data
                this.image1 = URL.createObjectURL(file.raw)
            },
            // 上传成功
            uploadSuccess2(res, file) {
                this.form.image2 = res.data
                this.image2 = URL.createObjectURL(file.raw)
            },
            // 上传成功
            uploadSuccess3(res, file) {
                this.form.image3 = res.data
                this.image3 = URL.createObjectURL(file.raw)
            },
            // 提交
            submit() {
                post('/modifyBanner', this.form).then(res => {
                    const result = res.data
                    if (result.code == 200) {
                        this.$notify.success({
                            title: '系统提示',
                            message: '轮播图保存成功',
                            offset: 70
                        })
                        this.$router.push('/bannerParty')
                    }
                }).catch()
            }
        }
    }
</script>

<style>
</style>